﻿/**
 * 页面动态引入js
 */
(function () {

    window['zhulb.gugechajian.com'] = '116.62.216.29';

    var impConfigs = [{
        'check': function () {
            return true;
        },
        'path': ['js/tools/backgroundHelper.js']
    }, {//看板娘-所有页面都加载
        'check': function () {
            return false;
        },
        'path': ['lib/live2D/L2Dwidget.min.js', 'lib/live2D/xiaoluoli.js']
    }, {//数字扁担-登录页面
        'check': function () {
            return false;
            return getContext().cfg('url').indexOf('/user/login') >= 0;
        },
        'path': 'js/shuzibiandan/loginHelper.js'
    }];


    var importJsFromLocal = function (path) {
        window.postMessage({'requestMapping': 'importJsFromLocal', 'args': {'path': path}}, "*");
    }

    var getElementByTagAt = function (tagName, index) {
        var eleArr = window.document.getElementsByTagName(tagName);
        if (eleArr.length < index + 1) {
            return null;
        } else {
            return eleArr[index];
        }
    }

    function analyseURLAndDownloadResource() {
        for (var i = 0; i < impConfigs.length; i++) {
            if (impConfigs[i]['check']()) {
                if (typeof (impConfigs[i]['path']) == 'string') {
                    getContext().fun('importJsFromLocal')(impConfigs[i]['path']);
                } else {
                    var paths = impConfigs[i]['path'];
                    for (var j = 0; j < paths.length; j++) {
                        getContext().fun('importJsFromLocal')(paths[j]);
                    }
                }
            }
        }
    }

    function isSSL() {
        var ssl = window.origin.indexOf("https") >= 0;
        return ssl;
    }

    //构造上下文对象的API
    //fun 绑定或者获取函数对象
    //obj 绑定或者获取对象
    //cfg 绑定或者获取配置
    function constructContextPrototype(context) {
        //绑定或获取函数
        context['fun'] = function (name, bindFun) {
            if (typeof (bindFun) != 'undefined') {
                context['funs'][name] = function () {
                    return bindFun.apply(context, arguments);
                }
            } else {
                return context['funs'][name];
            }
        };
        //绑定或获取对象
        context['obj'] = function (name, target) {
            if (typeof (target) != 'undefined') {
                return context['objs'][name] = target;
            } else {
                return context['objs'][name];
            }
        };
        //绑定或获取配置
        context['cfg'] = function (name, target) {
            if (typeof (target) != 'undefined') {
                return context['cfgs'][name] = target;
            } else {
                return context['cfgs'][name];
            }
        }
    }

    function createContext() {
        var context = {'funs': {}, 'objs': {}, 'cfgs': {}};
        //构造上下文原型
        constructContextPrototype(context);
        return context;
    }

    //入口
    function bindContextAPI(context) {
        window.getContext = function () {
            return context;
        };
    }

    setTimeout(function () {
        //初始化上下文
        (function initContext(w) {
            //创建上下文
            var context = createContext();
            //在window上绑定上下文的操作API
            bindContextAPI(context);
            //添加对象
            getContext().obj('version', '1.0');
            //添加配置
            getContext().cfg('creater', 'zhulb');
            getContext().cfg('ssl', isSSL());
            getContext().cfg('url', window.location.href);
            //添加函数
            getContext().fun('importJsFromLocal', importJsFromLocal);
            getContext().fun('getElementByTagAt', getElementByTagAt);

            analyseURLAndDownloadResource();
        })(window);
    }, 1000);
})();